<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/10/15
 * Time: 下午1:45
 */

namespace app\astrologer\lib;

use app\common\cachemodel\ZlcrAstr;
use app\zyhsapi\model\ZyhsConsultChatRecord;
use app\zyhsapi\model\ZyhsConsultChatRecordAstr;
use app\zyhsapi\model\ZyhsConsultChatRecordDetails;
use think\Db;
use think\queue\Job;

class DefaultReplyJob
{
    /**
     * 用户问答默认回复处理
     * @param Job $job
     * @param $jobData
     * @return void
     */
    public function reply(Job $job, $jobData)
    {
        $nowTime = time();
//        $jobData = [
//            'chat_id' => 'zyhs_consult_chat_record_details的ID',
//        ];

        $jobAttempts = $job->attempts();
        if ($jobAttempts > 2)
        {   // 失败两次 则删除
            $job->delete();
            return;
        }

        try
        {
            $consultChatUserModel = new ZyhsConsultChatRecord();
            $consultChatAstrModel = new ZyhsConsultChatRecordAstr();
            $consultChatRecordDetailsModel = new ZyhsConsultChatRecordDetails();

            // 获取当前问答的信息
            $chatDetailInfo = $consultChatRecordDetailsModel->getInfoById($jobData['chat_id'], ['app_type', 'uid', 'astr_id']);

            $astrInfo = (new ZlcrAstr())->getInfoById($chatDetailInfo['astr_id']);
            if (empty($astrInfo['extend']['default_reply']))
            {
                $job->delete();
                return;
            }

            // 获取用户与咨询师最后对话信息
            $userChatId = $consultChatUserModel->getChatIdByAstrId($chatDetailInfo['app_type'], $chatDetailInfo['uid'], $chatDetailInfo['astr_id']);
            $userChatInfo = $consultChatUserModel->getInfoById($userChatId, ['id', 'last_chat_detail_id', 'last_default_reply_time']);

            //查看当天是否已经自动回复过, 如果当天已经自动回复过则不再自动回复
            $defaultReplyDate = $userChatInfo['last_default_reply_time'] ? date('Ymd', $userChatInfo['last_default_reply_time']) : '';
            if ($defaultReplyDate === date('Ymd'))
            {
                $job->delete();
                return;
            }
            // 判断当前问答是否已回复
            if ($jobData['chat_id'] === $userChatInfo['last_chat_detail_id'])
            {
                $consultMsgData = [
                    'chat_id' => $userChatId,
                    'app_type' => $chatDetailInfo['app_type'],
                    'uid' => $chatDetailInfo['uid'],
                    'astr_id' => $chatDetailInfo['astr_id'],
                    'type' => 1,    //  1=>APP消息 2=>用户发送消息
                    'title' => '',
                    'content' => $astrInfo['extend']['default_reply'], // 默认回复
                    'data_type' => 12,   //  1=>普通消息  2=>图片  3=>语音咨询消息  4=>咨询问题  5=>咨询问题答案
                    'extend' => [],  //  语音咨询消息 ｜ 图片信息
                    'pid' => 0, // 待更新
                    'create_time' => $nowTime,
                ];
                $resData = $consultChatRecordDetailsModel->SaveData($userChatId, $consultMsgData);
                if ($resData)
                {
                    // 更新用户端 最后咨询信息
                    $consultChatUserModel->updateData($userChatId, [
                        'uid' => $chatDetailInfo['uid'],
                        'last_chat_detail_id' => $resData['id'],
                        'last_chat_detail_time' => $nowTime,
                        'last_default_reply_time' => $nowTime,
                        'msg_num' => Db::raw('msg_num + 1'),
                    ], $chatDetailInfo['app_type']);
                    $consultChatUserModel->setUserUnreadChatIdMsgNum($chatDetailInfo['app_type'], $chatDetailInfo['uid'], $userChatId, 1, 1);
                    $d = [
                        'last_chat_detail_id' => $resData['id'],
                        'last_chat_detail_time' => $nowTime,
                        'last_default_reply_time' => $nowTime,
                    ];
                    // 更新咨询师端 最后咨询信息
                    $consultChatAstrModel->astroSendUpdate($chatDetailInfo['app_type'], $chatDetailInfo['uid'], $chatDetailInfo['astr_id'], $d);
                }
            }
            $job->delete();
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()用户咨询默认回复异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
        }
    }
}
